package com.ruoyi.mp.controller;

import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.db.domain.system.PcRaise;
import com.ruoyi.mp.service.IPcRaiseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 众筹活动定时结算收益Controller
 * 
 * @author ruoyi
 * @date 2022-08-03
 */

@RestController
@RequestMapping("/mp/raise/job")
public class PcRaiseController extends BaseController
{
    private static Logger log = LoggerFactory.getLogger(PcRaiseController.class);
    @Autowired
    private IPcRaiseService pcRaiseService;
    @Autowired
    private RedisService redisService;


    /**
     * 查询众筹活动列表
     */
    @ApiOperation(value = "查询众筹活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pcRaise",value="众筹活动",paramType="form")
    })
    @GetMapping("/list")
    public Map<String,Object> list(PcRaise pcRaise)
    {
        try {
            startPage();
            List<PcRaise> list = pcRaiseService.selectPcRaiseList(pcRaise);
            BigDecimal share = pcRaiseService.getShare(pcRaise);
            Map<String,Object> map =new HashMap<>();
            map.put("shareAmount",share);
            map.put("page",getDataTable(list));
            map.put("code", HttpStatus.SUCCESS);
            return map;
        }catch (Exception e){
            log.error("众筹活动发生异常",e);
        }
        return null;
    }

    @ApiOperation(value = "查询众筹活动详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pcRaise",value="众筹活动",paramType="form")
    })
    @GetMapping(value = "/{raiseId}")
    public AjaxResult getInfo(@PathVariable("raiseId") Long raiseId)
    {
        try {
            PcRaise raise = pcRaiseService.selectPcRaiseById(raiseId);
            return AjaxResult.success(raise);
        }catch (Exception e){
            log.error("众筹活动发生异常",e);
            return AjaxResult.error(e.getMessage());
        }
    }

    // 每天凌晨两点执行一次
    @GetMapping("/settle")
    @Scheduled(cron= "0 0/3 * * * ?")
    public void demo(){
        System.out.println("计算活动");
        PcRaise raise = pcRaiseService.getNewOne();
        System.out.println(raise.getId());
        if(raise!=null&&raise.getIsOver()==0){
            System.out.println("进入");
            SimpleDateFormat sdfTwo = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
            String key = sdfTwo.format(raise.getContinueTime());
            if(redisService.hasKey("RAISE_"+raise.getRaiseType()+"_"+key)){
                System.out.println(redisService.getCacheObject("RAISE_"+raise.getRaiseType()+"_"+key).toString());
                return;
            }
            System.out.println(raise.getRaiseName()+"RAISE_"+raise.getRaiseType()+"_"+key);
            redisService.setCacheObject("RAISE_"+raise.getRaiseType()+"_"+key,raise.getId());
            System.out.println("存入redis"+raise.getRaiseName()+"RAISE_"+raise.getRaiseType()+"_"+key);
            Timer timer = new Timer();
            timer.schedule(new RemindTask(), raise.getContinueTime());
        }

    }

    // 每天凌晨两点执行一次
    @GetMapping("/settleTest")
    @Scheduled(cron = "0 0/30 * * * ?")
    public void settleSuccess(){
        System.out.println("计算成功的活动");
        pcRaiseService.settleSucByOverTime();
    }



    class RemindTask extends TimerTask {

        public void run() {
            System.out.println("开始活动结算任务localDateTime:"+new Date());
            //结算时间结算收益
           pcRaiseService.settleByOverTime();;
        }
    }


}
